<!DOCTYPE HTML>
<html>
<head>
    {include file="common:meta"}
    <link type="text/css" rel="stylesheet" href="/static/admin/plugin/bootstrap-fileinput/css/fileinput.css"/>
</head>
<body>
<div class="page-container">
    <div class="ibox-content" id="toolbar">
        <form action="" class="form-inline" id="" data-url="">
            <div class="form-group">
                <label class="control-label" for="key">状态:</label>
                <select id="key" class="form-control" name="key">
                    <option value="">请选择</option>
                    <option value="id">主键</option>
                    <option value="u_id">ID</option>
                    <option value="nickname">用户名</option>
                    <option value="family_name">家族名</option>
                </select>
            </div>

            <div class="form-group">
                <label class="control-label" for="value">输入:</label>
                <input class="form-control" id="value" type="text" name="value">
            </div>
            <button type="button" class="btn btn-primary" id="eventQuery">查询</button>
            <button type="button" onclick="location.replace(location.href);" class="btn btn-success ">刷新</button>
        </form>

        <div style="margin-top:5px;">
            <label class="control-label" >
                <select name="" id="choose-room" class="form-control">
                    <option value="all">全部机器人</option>
                    <option value="not-enter">未进入房间的机器人</option>
                </select>
            </label>
            <button class="btn btn-w-m btn-success search-room">搜索</button>
        </div>

    </div>
    <div class=" float-e-margins">
        <div class="ibox-content">
            <div class="tabs-container">
                <ul class="nav nav-tabs">
                    <li class="active"><a data-toggle="tab" href="#tab-1" aria-expanded="true">用户列表</a>
                    </li>
                    <li class=""><a data-toggle="tab" href="#tab-2" aria-expanded="true">用户添加</a>
                    </li>
                </ul>
                <div class="tab-content">
                    <div class=" tab-pane active" id="tab-1">
                        <table id="tb_departments"></table>
                    </div>
                    <div class=" tab-pane" id="tab-2">
                        <div class="ibox-content">
                            <form class="form-horizontal"  id="form"   data-url="/RobotAdd" enctype="multipart/form-data">

                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="nickname">
                                        <span class="c-red">*</span>用户昵称：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <input type="text" class="form-control" id="nickname" value="" name="nickname">
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label">
                                        <span class="c-red">*</span>性别：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <div class="radio i-checks">
                                            <label for=""><input type="radio" class=""  name="sex" value="1" checked>男</label>
                                            <label for=""><input type="radio" class=""  name="sex"  value="2">女</label>
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="signature">
                                        <span class="c-red">*</span>签名：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <textarea name="signature" id="signature" cols="30" rows="10" class="form-control"></textarea>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="pic">
                                        <span class="c-red">*</span>头像：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <input id="pic"  class="form-control" type="file" name="pic">
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="">
                                        <span class="c-red">*</span>生日：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <input id=""  class="form-control date-search" type="text" name="birthday">
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="hobby">
                                        <span class="c-red">*</span>爱好：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <input type="text" class="form-control" id="hobby" value="" name="hobby">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
                                        <button class="btn btn-primary" type="button" onclick="commonSubmit(this)">添加
                                        </button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="common:footer"}
<script type="text/javascript" src="/static/admin/plugin/bootstrap-fileinput/js/fileinput.js"></script>

<script type="text/javascript" src="/static/admin/plugin/bootstrap-fileinput/js/locales/zh.js"></script>
<script>
    $(document).ready(function () {
        initTable();
        display();
        displayAll($("#pic"));
    });
    function displayAll(obj) {
        obj.fileinput({
            language: 'zh', //设置语言

//            uploadUrl: "/GiftAdd", //上传的地址

            allowedFileExtensions: ['apk', 'jpg', 'png', 'jpeg'],//接收的文件后缀

            uploadAsync: false, //默认异步上传

            showUpload: false, //是否显示上传按钮

            showRemove: true, //显示移除按钮

            showPreview: true, //是否显示预览

            browseClass: "btn btn-primary", //按钮样式

            maxFileCount: 1, //表示允许同时上传的最大文件个数

            enctype: 'multipart/form-data',

            validateInitialCount: true,

            previewFileIcon: "<iclass =" + 'glyphicon glyphicon-king' + "></i>",

            msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！"
        }).on("filebatchuploadsuccess", function (event, data) {
            if (data) {
                $("#img_address").val(data.response);
            }
        });
    }

    function initTable() {
        $('#tb_departments').bootstrapTable({
            url: '/CustomerRobotIndexData',         //请求后台的URL（*）
            dataField: "data",
            method: 'post',                      //请求方式（*）
            striped: true,                      //是否显示行间隔色
            cache: true,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: true,                   //是否显示分页（*）
            queryParams: queryParams,//传递参数（*）
            showHeader: true,
            showToggle: true,
            showRefresh: true,
            sortStable: true,
            sortName: 'id',
            sortOrder: "desc",                   //排序方式
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,                       //初始化加载第一页，默认第一页
            pageSize: 10,                       //每页的记录行数（*）
            pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
            showColumns: true,                  //是否显示所有的列
            minimumCountColumns: 2,             //最少允许的列数
            toolbar: '#toolbar',
            columns: [{
                field: 'id',
                title: '主键',
                align: 'center',
                sortable: true
            }, {
                field: 'u_id',
                title: 'ID',
                align: 'center',
                sortable: true,
                editable: {
                    type: 'number',
                    title: '更改用户信息（ID）'
                }
            }, {
                field: 'nickname',
                title: '用户名',
                align: 'center',
            },{
                field: 'u_phone',
                title: '手机号',
                align: 'center',
                formatter: function (value, row, index) {

                    if(row.u_phone == null ){
                        return "无";
                    }else{
                        return row.u_phone;
                    }
                }
            }, {
                field: 'followers',
                title: '粉丝数',
                align: 'center',
            }, {
                field: 'friends',
                title: '关注数',
                align: 'center',
            }, {
                field: 'family_name',
                title: '家族名称',
                align: 'center',
            },{
                field: 'last_login',
                title: '上次登录时间',
                align: 'center',
            }, {
                field: 'create_time',
                title: '注册时间',
                align: 'center',
            }, {
                field: 'Button',
                title: '操作',
                events: operateEvents,
                formatter: operateFormatter
            }],
            //保存的使用
            onEditableSave: function (field, row, oldValue, $el) {
                //可进行异步操作
                layer.confirm('确认修改吗？，请先检查是否有重名',{
                    title:['提示','font-size:18px;font-family:宋体;font-weight:700;'],
                    btn: ['确定','取消'] //按钮
                },function(){
                    $.ajax({
                        type: "get",
                        url: "/UserEdit",
                        data:{
                            'user_id':row.id,
                            'u_id':row.u_id
                        } ,
                        dataType: 'JSON',
                        success: function (re) {
                            if (re.code === 100) {
                                layer.msg(re.msg,{
                                    icon: 1,
                                    time: 2000
                                });
                            } else {
                                layer.msg(re.msg,{
                                    icon:2,
                                    time:1000
                                });
                                $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
                            }
                        },
                        error: function () {
                            alert('编辑失败');
                        }
                    });
                })

            }
        });
    }

    //得到查询的参数

    function queryParams(params) {
        params.offset = params.offset / params.limit + 1;
        params.key = $('#key').val();
        params.value = $('#value').val();
        console.log(JSON.stringify(params));
        return params;
    }

    function operateFormatter(value, row, index) {
        if(row.status == 1){
            var status = '禁封账号';
            var control = 'ban-btn';
            var color = 'btn-danger';
        }else if(row.status == 2){
            var status = '解封账号';
            var control = 'rid-btn';
            var color = 'btn-primary';
        }

        if(row.fa_id != null){
            var sho = 'none';
        }else{
            var sho = 'inline-block';
        }

        if(row.f_id != null){
            var sh = 'none';
        }else{
            var sh = 'inline-block';
        }

        return [
            '<button type="button"  class="userInfo-btn btn btn-warning  btn-sm" style="margin-right:15px;">查看用户信息</button>',
            '<button type="button"  class="'+control +' btn '+color+'  btn-sm" style="margin-right:15px;">'+status+'</button>',
            '<button type="button"  class="build-family btn btn-primary  btn-sm" style="margin-right:15px; display:'+sh+';">开通家族</button>',
            '<button type="button" id="agree" class="agree-family btn-primary  btn-sm" style="margin-right:15px;display:'+sho+';">创建预审家族</button>',
        ].join('');
    }

    window.operateEvents = {
        'click .userInfo-btn':function(e,value,row,index){
            layer.open({
                type: 2,
                title: ['用户信息','font-size:18px;font-family:宋体;font-weight:700;'],
                shadeClose: true,
                shade: false,
                maxmin: true, //开启最大化最小化按钮
                area: ['900px', '600px'],
                content:"/UserInfo?user_id=" + row.id,
                offset:'auto',
                scrollbar:false
            });
        },

        'click .ban-btn':function(e,value,row,index){
            layer.open({
                type: 2,
                title: ['禁封账号','font-size:18px;font-family:宋体;font-weight:700;'],
                shadeClose: true,
                shade: false,
                maxmin: true, //开启最大化最小化按钮
                area: ['900px', '600px'],
                content:"/UserBan?user_id=" + row.id,
                offset:'auto',
                scrollbar:false,
                end: function () {
                    $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
                }
            });
        },

        'click .rid-btn':function(e,value,row,index){
            layer.confirm('确定禁封吗？',{
                title:['提示','font-size:18px;font-family:楷体;font-weight:700;'],
                btn: ['确定','取消'] //按钮
            },function(){
                $.ajax({
                    type:'get',
                    url:"/UserRid?user_id=" + row.id,
                    async:true,
                    dataType:'json',
                    success:function(result){
                        if(result.code == 100){
                            layer.msg(result.msg,{
                                icon:1,
                                time:1000
                            });
                            $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
                        }
                        else{
                            layer.msg(result.msg,{
                                icon:2,
                                time:1000
                            });
                        }
                    },
                    error:function(err){
                        layer.msg('服务器开小差了！',{
                            time:1500
                        });
                    }
                });
            });
        },

        'click .build-family':function(e,value,row,index){
            layer.confirm('确定要开通家族吗？',{
                title:['提示','font-size:18px;font-family:楷体;font-weight:700;'],
                btn: ['确定','取消'] //按钮
            },function(){
                $.ajax({
                    type:'get',
                    url:"/UserBuildFamily?user_id=" + row.id,
                    async:true,
                    dataType:'json',
                    success:function(result){
                        if(result.code === 100){
                            layer.msg(result.msg,{
                                icon:1,
                                time:1000
                            });
                            $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
                        }
                        else{
                            layer.msg(result.msg,{
                                icon:2,
                                time:1000
                            });
                        }
                    },
                    error:function(err){
                        layer.msg('服务器开小差了！',{
                            time:1500
                        });
                    }
                });
            });
        },

        'click .agree-family':function(e,value,row,index){

            layer.open({
                type: 2,
                title: ['预审家族','font-size:18px;font-family:宋体;font-weight:700;'],
                shadeClose: true,
                shade: false,
                maxmin: true, //开启最大化最小化按钮
                area: ['900px', '600px'],
                content:"/UserBuildAgreeFamily?user_id=" + row.id,
                offset:'auto',
                scrollbar:false,
                end: function () {
                    $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
                }
            });

        }
    };

    //获取机器人所在的房间
    function display(){
        $.ajax({
            type:'get',
            url:"/getRobotRoom?",
            async:true,
            dataType:'json',
            success:function(result){
                console.log(result);
                var htmlText = '';
                for(var i in result){
                    htmlText += '<option value='+result[i].id+'>'+result[i].r_name+'</option>';
                }
                $('#choose-room').append(htmlText);
            },
            error:function(err){
                layer.msg('服务器开小差了！',{
                    time:1500
                });
            }
        });
    }

    $('.search-room').click(function(){

        var item = $('#choose-room').val();
        console.log(typeof(item));
        console.log(item);
        //查看全部机器人
        if(item === 'all'){
            $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
            return true;
        }


        //查看未进入房间的机器人
        if(item === 'not-enter'){
            layer.open({
                type: 2,
                title: ['未进入房间的机器人','font-size:18px;font-family:宋体;font-weight:700;'],
                shadeClose: true,
                shade: false,
                maxmin: true, //开启最大化最小化按钮
                area: ['900px', '600px'],
                content:"NotEnterRoomRobot" ,
                offset:'auto',
                scrollbar:false,
                end:function(){
                    window.location.reload();
                }
            });
        }
        //查看含有机器人的房间
        else{
            console.log(item);
            layer.open({
                type: 2,
                title: ['含有机器人的房间','font-size:18px;font-family:宋体;font-weight:700;'],
                shadeClose: true,
                shade: false,
                maxmin: true, //开启最大化最小化按钮
                area: ['900px', '600px'],
                content:"HaveRobotRoom?room_id="+item ,
                offset:'auto',
                scrollbar:false
            });
        }
    });
    // 搜索按钮触发事件
    $(function () {
        $("#eventQuery").click(function () {
            $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
        });

    });

    //时间搜索插件
    layui.use('laydate', function () {
        var laydate = layui.laydate;
        lay('.date-search').each(function () {
            laydate.render({
                elem: this,
                type: 'datetime',
                trigger: 'click',
                format: 'yyyy-MM-dd HH:mm:ss'
            });
        });
    });
</script>
</body>
</html>